//
//  PlaceDetailViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "PlaceDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "LSAppTools.h"
#import "PlaceDetailModel.h"
#import "PicTableViewCell.h"
#import "LabelTableViewCell.h"
#import "Masonry.h"
@interface PlaceDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) NSMutableArray *placeDetailArray;


@end

@implementation PlaceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.travelSubejctModel.name;
    
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 40 * HEI) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.myTableView];
    [self.myTableView registerClass:[LabelTableViewCell class] forCellReuseIdentifier:@"LabelTableViewCell"];
    [self.myTableView registerClass:[PicTableViewCell class] forCellReuseIdentifier:@"PicTableViewCell"];
    self.titleView = [[UIImageView alloc] init];
    self.myTableView.tableHeaderView = self.titleView;
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(200 * HEI);
        
    }];
    
    UIImageView *titleImageView = [[UIImageView alloc] init];
    [self.titleView addSubview:titleImageView];
    [titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(200 * HEI);
        
    }];
    titleImageView.contentMode = UIViewContentModeScaleAspectFill;
    titleImageView.clipsToBounds = YES;
    [titleImageView sd_setImageWithURL:[NSURL URLWithString:self.travelSubejctModel.image_url] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    
    UILabel *nameLable = [[UILabel alloc] init];
    [self.titleView addSubview:nameLable];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((WIDTH - 300 * WID) / 2);
        make.top.mas_equalTo(70 * HEI);
        make.width.mas_equalTo(300 * WID);
        make.height.mas_equalTo(30 * HEI);
        
    }];
    nameLable.text = self.travelSubejctModel.name;
    nameLable.textAlignment = NSTextAlignmentCenter;
    nameLable.textColor = [UIColor whiteColor];
    
    UILabel *lLabel = [[UILabel alloc] init];
    [self.titleView addSubview:lLabel];
    [lLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((WIDTH - 300 * WID) / 2);
        make.top.mas_equalTo(100 * HEI);
        make.width.mas_equalTo(300 * WID);
        make.height.mas_equalTo(20 * HEI);
        
    }];
    lLabel.text = @"_______________。_______________";
    lLabel.textColor = [UIColor whiteColor];
    lLabel.textAlignment = NSTextAlignmentCenter;
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [self.titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((WIDTH - 300 * WID) / 2);
        make.top.mas_equalTo(140 * HEI);
        make.width.mas_equalTo(300 * WID);
        make.height.mas_equalTo(30 * HEI);
        
    }];
    titleLabel.text = self.travelSubejctModel.title;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    

    
    [self getAFNetWorkingData];
    
    
    self.placeDetailArray = [NSMutableArray array];
}

-(void)getAFNetWorkingData
{
    
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/articles/%@.json?page=1",self.travelSubejctModel.nameId];
    
    [LSAppTools getDataWithURL:url blcok:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"article_sections"];
        
        for (NSMutableDictionary *dic in array) {
            PlaceDetailModel *placeDetailModel = [[PlaceDetailModel alloc] init];
            [placeDetailModel setValuesForKeysWithDictionary:dic];
            [self.placeDetailArray addObject:placeDetailModel];
            
            
        }
        [self.myTableView reloadData];

        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.placeDetailArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PicTableViewCell *picCell = [tableView dequeueReusableCellWithIdentifier:@"PicTableViewCell"];
    LabelTableViewCell *labelCell = [tableView dequeueReusableCellWithIdentifier:@"LabelTableViewCell"];
    PlaceDetailModel *placePlaceModel = [[PlaceDetailModel alloc] init];
    placePlaceModel = [self.placeDetailArray objectAtIndex:indexPath.row];
    picCell.selectionStyle = UITableViewCellSelectionStyleNone;
    labelCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([placePlaceModel.image_url  isEqual: @""]) {
        labelCell.placeDetailModel = placePlaceModel;
        return labelCell;
    }else{
        picCell.placeDetailModel = placePlaceModel;
        return picCell;
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PlaceDetailModel *placeDetailModel = [[PlaceDetailModel alloc] init];
    placeDetailModel = [self.placeDetailArray objectAtIndex:indexPath.row];
    CGFloat height;
    if ([placeDetailModel.image_url isEqual:@""]) {
        height = [LabelTableViewCell heightForContent:placeDetailModel.detailDescription];
        return height + 60 * HEI;
    }else{
        height = [PicTableViewCell heightForContent:placeDetailModel.detailDescription] + 367 / [(placeDetailModel.image_width)floatValue] * [(placeDetailModel.image_height)floatValue];
        return height + 30 * HEI;
    }
    
}


-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
